package neo.app.service;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Map;

import neo.core.common.PagingList;
import neo.core.util.MapUtil;

public class TaskService extends BaseService {

	//获取任务单 列表
	private static final String SQL_GET_ALL_TASK = "select t.*,(select client_comp_name from ken_client where t.task_client_id=client_id) as client_name,(select client_comp_name from ken_client where t.task_receiver_id=client_id) as receiver_name from ken_task t order by task_flag";

	/**
	 * 获取任务单 列表
	 * @return
	 */
	public PagingList getAllTask() {
		return getPagingList(SQL_GET_ALL_TASK);
	}

	//获取所有用户列表
	private static final String SQL_GET_ALL_CLIENT = "select client_id,client_comp_name from ken_client";

	/**
	 * 获取所有用户列表
	 * @return
	 */
	public List getAllClient() {
		return jt.queryForList(SQL_GET_ALL_CLIENT);
	}

	//获取送货方式 列表
	private static final String SQL_GET_ALL_DELIVER_TYPE = "select * from ken_public where public_type='送货方式'";

	/**
	 * 获取送货方式 列表
	 * @return
	 */
	public List getAllDeliverType() {
		return jt.queryForList(SQL_GET_ALL_DELIVER_TYPE);
	}

	//获取付款方式 列表
	private static final String SQL_GET_ALL_PAY_TYPE = "select * from ken_public where public_type='付款方式'";

	/**
	 * 获取付款方式 列表
	 * @return
	 */
	public List getAllPayType() {
		return jt.queryForList(SQL_GET_ALL_PAY_TYPE);
	}

	//获取路线 列表
	private static final String SQL_GET_ALL_LINE = "select concat(line_begin,' - ',line_end) as line from ken_line";

	/**
	 * 获取路线 列表
	 * @return
	 */
	public List getAllLine() {
		return jt.queryForList(SQL_GET_ALL_LINE);
	}

	//添加任务单
	private static final String SQL_INSERT_TASK = "insert into ken_task values(task_id,?,?,?,?,?,?,?,?,?,0,?,?)";

	/**
	 * 添加任务单
	 * @param parameters
	 * @return
	 */
	public boolean addTask(Map map) {
		//1、获取值
		String task_no = MapUtil.getStringFromMap(map, "task_no");
		int task_client_id = MapUtil.getIntFromMap(map, "task_client_id");
		int task_receiver_id = MapUtil.getIntFromMap(map, "task_receiver_id");
		String task_deliver_type = MapUtil.getStringFromMap(map, "task_deliver_type");
		String task_pay_type = MapUtil.getStringFromMap(map, "task_pay_type");
		String task_transfer_path = MapUtil.getStringFromMap(map, "task_transfer_path");
		double task_transfer_weight = Double.parseDouble(MapUtil.getStringFromMap(map, "task_transfer_weight"));
		String task_transfer_fee = MapUtil.getStringFromMap(map, "task_transfer_fee");//费用可能需要处理 类型
		String task_have_payed_fee = MapUtil.getStringFromMap(map, "task_have_payed_fee");//费用可能需要处理 类型
		Date dt = new Date();//如果不需要格式,可直接用dt,dt就是当前系统时间
		DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置显示格式
		String task_transfer_date = "";
		task_transfer_date = df.format(dt);//用DateFormat的format()方法在dt中获取并以yyyy/MM/dd HH:mm:ss格式显示
		String task_remark = MapUtil.getStringFromMap(map, "task_remark");

		//2、检查有效性

		//3、sql语句

		//4、执行sql
		if (jt.update(SQL_INSERT_TASK, new Object[] { task_no, task_client_id, task_pay_type, task_receiver_id,
				task_deliver_type, task_transfer_path, task_transfer_fee, task_have_payed_fee, task_transfer_date,
				task_remark, task_transfer_weight}) > 0) {
			return true;
		}
		return false;
	}

	//根据task_id 查询 任务单
	private static final String SQL_GET_TASK_BY_ID = "select t.*,(select client_comp_name from ken_client where t.task_client_id=client_id) as client_name,(select client_comp_name from ken_client where t.task_receiver_id=client_id) as receiver_name from ken_task t where task_id=?";

	/**
	 * 根据task_id 查询 任务单
	 * @param intFromMap
	 * @return
	 */
	public Map findTaskById(int task_id) {
		return jt.queryForMap(SQL_GET_TASK_BY_ID, new Object[]{task_id });
	}

	//修改任务单
	private static final String SQL_UPDATE_TASK_BY_ID = "update ken_task set task_deliver_type=?,task_pay_type=?,task_transfer_path=?,task_transfer_weight=?,task_transfer_fee=?,task_have_payed_fee=?,task_remark=? where task_id=?";
	
	/**
	 * 修改任务单
	 * @param parameters
	 * @return
	 */
	public boolean editTask(Map map) {
		//1、获取值
		int task_id = MapUtil.getIntFromMap(map, "task_id");
		String task_deliver_type = MapUtil.getStringFromMap(map, "task_deliver_type");
		String task_pay_type = MapUtil.getStringFromMap(map, "task_pay_type");
		String task_transfer_path = MapUtil.getStringFromMap(map, "task_transfer_path");
		double task_transfer_weight = Double.parseDouble(MapUtil.getStringFromMap(map, "task_transfer_weight"));
		String task_transfer_fee = MapUtil.getStringFromMap(map, "task_transfer_fee");//费用可能需要处理 类型
		String task_have_payed_fee = MapUtil.getStringFromMap(map, "task_have_payed_fee");//费用可能需要处理 类型
		String task_remark = MapUtil.getStringFromMap(map, "task_remark");
		//2、检查数据有效性
		//3、sql语句
		//4、执行sql
		if(jt.update(SQL_UPDATE_TASK_BY_ID, new Object[]{task_deliver_type,task_pay_type,task_transfer_path,task_transfer_weight,task_transfer_fee,task_have_payed_fee,task_remark,task_id }) > 0){
			return true;
		}
		return false;
	}

	//获取未执行 状态任务单列表
	private static final String SQL_GET_UNDO_TASK = "select t.*,(select client_comp_name from ken_client where t.task_client_id=client_id) as client_name,(select client_comp_name from ken_client where t.task_receiver_id=client_id) as receiver_name from ken_task t where task_flag=0";
	
	/**
	 * 获取未执行 状态任务单列表
	 * @return
	 */
	public PagingList getUnDoTasks() {
		return getPagingList(SQL_GET_UNDO_TASK);
	}
	
	//审核任务单
	private static final String SQL_UPDATE_TASK_FLAG = "update ken_task set task_flag=1 where task_id=?";

	/**
	 * 审核任务单
	 */
	public void checkTask(Map map) {
		jt.update(SQL_UPDATE_TASK_FLAG, new Object[]{MapUtil.getIntegerObjectFromMap(map, "task_id")});
	}
	
	//获取未执行 状态的任务单列表 并按 路线终点站分组
	private static final String SQL_GET_UNDO_TASKS_ORDER_BY_PATH = "select t.*,(select client_comp_name from ken_client where t.task_client_id=client_id) as client_name,(select client_comp_name from ken_client where t.task_receiver_id=client_id) as receiver_name from ken_task t where task_flag=0 order by substring_index(task_transfer_path,'-',-1)";
	
	/**
	 * 获取未执行 状态的任务单列表 并按 路线终点站分组
	 * @return
	 */
	public PagingList getUnDoTasksOrderByPath() {
		return getPagingList(SQL_GET_UNDO_TASKS_ORDER_BY_PATH);
	}
	
	//获取执行中 状态的任务单列表
	private static final String  SQL_GET_ALL_DOING_TASK = "select t.*,(select client_comp_name from ken_client where t.task_client_id=client_id) as client_name,(select client_comp_name from ken_client where t.task_receiver_id=client_id) as receiver_name from ken_task t where task_flag=1 order by task_transfer_date asc";
	
	/**
	 * 获取执行中 状态的任务单列表 
	 */
	public PagingList getDoingTasks() {
		return getPagingList(SQL_GET_ALL_DOING_TASK);
	}
	
	//付款
	private static final String SQL_UPDATE_PAYED_FEE = "update ken_task set task_have_payed_fee=? where task_id=?";
	/**
	 * 付款
	 * @param parameters
	 */
	public void pay(Map map) {
		jt.update(SQL_UPDATE_PAYED_FEE, new Object[]{Double.parseDouble(MapUtil.getStringFromMap(map, "task_have_payed_fee")), MapUtil.getStringFromMap(map, "task_id") });
	}
	
	//结单
	private static final String SQL_UPDATE_TASK_FLAG_DONE = "update ken_task set task_flag=2 where task_id=?";
	
	/**
	 * 结单
	 * @param parameters
	 */
	public void endTask(Map map) {
		jt.update(SQL_UPDATE_TASK_FLAG_DONE, new Object[]{MapUtil.getStringFromMap(map, "task_id")});
	}

}
